package com.zhangkunhao.notes.web.servlet;

import com.alibaba.fastjson.JSON;
import com.zhangkunhao.notes.entity.Do.Answer;
import com.zhangkunhao.notes.entity.bo.Message;
import com.zhangkunhao.notes.service.AnswerServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 回答的controller
 *
 * @author 20642
 */
@WebServlet("/answer/*")
public class AnswerServlet extends BaseDaoServlet {

    public void answerQuestion(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.接收提出的问题
        String params = request.getReader().readLine();

        //2.转为对象
        Answer answer = JSON.parseObject(params, Answer.class);
        System.out.println(answer + "控制层的answer");

        //3.调用service层
        Message<Answer> answerQuestionMessage = new AnswerServiceImpl().answerQuestion(answer);
        System.out.println(answerQuestionMessage.getResult());

        //4.转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(answerQuestionMessage));
    }
}
